package com.fenghuajueli.module_user.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.fenghuajueli.libbasecoreui.recycler.setOnFastClickListener
import com.fenghuajueli.module_user.databinding.FragmentUserMineBinding
import com.fenghuajueli.module_user.network.result.TemplateResult
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 * @Author: sz_ijj
 * @Date: 2024-01-06 16:32
 **/
class XmlMineFragment : BaseMineFragment() {
    private var binding: FragmentUserMineBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentUserMineBinding.inflate(layoutInflater, container, false)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.apply {
            userInfoContainer.setOnFastClickListener { toUserInfoContainer() }
            stVipContainer.setOnFastClickListener { toVipContainer() }
            stToComment.setOnFastClickListener { toComment() }
            stUserFeedBack.setOnFastClickListener { toUserFeedBack() }
            stClearCache.setOnFastClickListener { toClearCache() }
            stAboutUs.setOnFastClickListener { toAboutUs() }
            stContactServer.setOnFastClickListener { toContactServer() }
            stAllAgreement.setOnFastClickListener { toAllAgreement() }
            stReportFeedBack.setOnFastClickListener { toReportFeedBack() }
            stInfoCollectList.setOnFastClickListener { toInfoCollectList() }
            stThirdSdkList.setOnFastClickListener { toThirdSdkList() }
        }

        lifecycleScope.launch {
            launch {
                viewModel.userUiState
                    .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
                    .collectLatest { uiState ->
                        binding?.apply {
                            stVipContainer.isVisible = uiState.payStatus
                            ivUserVipStatus.isVisible = uiState.payStatus
                            userInfoContainer.isVisible = uiState.payStatus

                            Glide.with(this@XmlMineFragment).load(
                                if (uiState.isLocalHead) {
                                    com.fenghuajueli.module_user.R.mipmap.icon_default_avatar
                                } else {
                                    uiState.headIcon
                                }
                            ).into(cvUserHead)

                            vRedDot.isVisible = uiState.mDot

                            tvUserName.text = if (uiState.isLogin) {
                                viewModel.getUserName()
                            } else {
                                "登录/注册"
                            }

                            if (uiState.isVip) {
                                ivUserVipStatus.setImageResource(
                                    com.fenghuajueli.module_user.R.mipmap.icon_members_02
                                )
//                            stVipContainer.setImageResource(
//                                com.fenghuajueli.module_user.R.mipmap.user_mine_membershipcard_02
//                            )
                            } else {
                                ivUserVipStatus.setImageResource(
                                    com.fenghuajueli.module_user.R.mipmap.icon_members_01
                                )
//                            stVipContainer.setImageResource(
//                                com.fenghuajueli.module_user.R.mipmap.user_mine_membershipcard_01
//                            )
                            }

                            if (uiState.icp.isNotBlank()) {
                                tvICP.isVisible = true
                                tvICP.text = uiState.icp
                            }
                        }
                    }
            }

            launch {
                viewModel.templateResult
                    .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
                    .collectLatest { templateResult ->
                        val uiState = viewModel.userUiState.value
                        binding?.apply {
                            Glide.with(this@XmlMineFragment).load(
                                when (templateResult) {
                                    is TemplateResult.Success -> {
                                        if (uiState.isVip)
                                            templateResult.data.images.user_pic_member_opened
                                        else
                                            templateResult.data.images.user_pic_member_notopened
                                    }

                                    else -> {
                                        if (uiState.isVip)
                                            com.fenghuajueli.module_user.R.mipmap.user_mine_membershipcard_02
                                        else
                                            com.fenghuajueli.module_user.R.mipmap.user_mine_membershipcard_01
                                    }
                                }
                            ).into(stVipContainer)
                        }
                    }
            }
        }
    }
}